home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Magazine / C_Tutorial / Part-12 / dt0 / loadsave.c < prev    next >
C/C++ Source or Header  |  1998-04-12  |  6KB  |  212 lines

  1. #include "loadsave.h"
  2. #include "bitmap.h"
  3. #include "drawwin.h"
  4. #include "gui.h"
  5. #include "main.h"
  6. #include "datatype.h"
  7.  
  8. #include<libraries/asl.h>
  9. #include<intuition/intuition.h>
  10. #include<workbench/workbench.h>
  11.  
  12. #include<string.h>
  13. #include<stdio.h>
  14.  
  15. #include<clib/asl_protos.h>
  16. #include<clib/dos_protos.h>
  17. #include<clib/graphics_protos.h>
  18. #include<clib/icon_protos.h>
  19. #include<clib/intuition_protos.h>
  20. #include<clib/datatypes_protos.h>
  21.  
  22. #include "iff.h"
  23.  
  24. /* Global handles for our requesters */
  25. static struct FileRequester* loadreq = NULL;
  26. static struct FileRequester* savereq = NULL;
  27.  
  28. /* Open an ASL load file requester */
  29. int load()
  30. {
  31.     /* Allocate the requester if we haven't already */
  32.     if(loadreq == NULL)
  33.         loadreq = (struct FileRequester*)
  34.             AllocAslRequestTags(ASL_FileRequest,
  35.                                                     ASLFR_TitleText,            "Load File",
  36.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS,
  37.                                                     ASLFR_InitialPattern,    "#?.iff",
  38.                                                     TAG_DONE);
  39.     if(loadreq)
  40.     {
  41.         struct Window* win = getDrawWin();
  42.         if(AslRequestTags(loadreq, ASLFR_Window, win, TAG_DONE))
  43.         {
  44.             char filename[MAXFILENAME];
  45.             /* Create complete filename from ASL's dir and file */
  46.             strcpy(filename, loadreq->rf_Dir);
  47.             if(AddPart(filename, loadreq->rf_File, MAXFILENAME))
  48.                 return loadfile(filename);
  49.             else
  50.                 printf("Error: could not make filename\n");
  51.         }
  52.         /* else: requester was cancelled */
  53.     }
  54.     else
  55.         printf("Error: could not allocate ASL (load) file request\n");
  56.     /* If we get this far then there was no fatal error */
  57.     return TRUE;
  58. }
  59.  
  60. int loadfile(char* filename)
  61. {
  62.     if(useDT())
  63.         return loadDT(filename);
  64.     else  /* Do old IFF library stuff */
  65.     {
  66.         /* Record any fatal errors */
  67.         int going = TRUE;
  68.         IFFL_HANDLE handle;
  69.         /* Try to open the IFF file */
  70.         if(handle = IFFL_OpenIFF(filename, IFFL_MODE_READ))
  71.         {
  72.             UWORD colortable[256];
  73.             /* Get colour information */
  74.             LONG count = IFFL_GetColorTab(handle, colortable);
  75.             /* Get display information */
  76.             ULONG displayid = IFFL_GetViewModes(handle);
  77.             /* Get picture information */
  78.             struct IFFL_BMHD* bmhd = IFFL_GetBMHD(handle);
  79.             struct Window* win = getDrawWin();
  80.             /* Try to adjust the screen to fit */
  81.             if(bmhd)
  82.             {
  83.                 struct EasyStruct myreq = { sizeof(struct EasyStruct),
  84.                                                                         0,
  85.                                                                         "Screen Change Confirmation",
  86.                                                                         "The selected image is %ldx%ldx%ld.\n"
  87.                                                                             "Do you wish to change the current\n"
  88.                                                                             "screen to match this?",
  89.                                                                         "Yes|No" };
  90.                 /* Only change the GUI if the user wants to */
  91.                 if(EasyRequest(win, &myreq, NULL,
  92.                                              bmhd->w, bmhd->h, bmhd->nPlanes))
  93.                 {
  94.                     closeGUI();
  95.                     /* If this fails, our local win will then be set to NULL */
  96.                     openGUI(bmhd->nPlanes, bmhd->w, bmhd->h, displayid);
  97.                     win = getDrawWin();
  98.                 }
  99.             }
  100.             if(win)
  101.             {
  102.                 /* Change screen colours */
  103.                 LoadRGB4(&(win->WScreen->ViewPort), colortable, count);
  104.                 /* If we can load the picture, update window's display */
  105.                 if(IFFL_DecodePic(handle, getBitmap()))
  106.                 {
  107.                     CopySBitMap(win->WLayer);
  108.                     setModified(FALSE);
  109.                 }
  110.                 else
  111.                     printf("Error: could not decode IFF picture\n");
  112.             }
  113.             else
  114.                 going = FALSE;  /* The only fatal error */
  115.             IFFL_CloseIFF(handle);
  116.         }
  117.         else
  118.             printf("Error: could not open IFF file\n");
  119.         return going;
  120.     }
  121. }
  122.  
  123. /* Open an ASL save file requester */
  124. void save()
  125. {
  126.     /* Another way of saying "allocate if we haven't already" */
  127.     if(savereq ||
  128.         (savereq = (struct FileRequester*)
  129.             AllocAslRequestTags(ASL_FileRequest,
  130.                                                     ASLFR_TitleText,            "Save File",
  131.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS | FRF_DOSAVEMODE,
  132.                                                     ASLFR_InitialPattern,    "#?.iff",
  133.                                                     ASLFR_InitialFile,        "picture.iff",
  134.                                                     TAG_DONE)))
  135.     {
  136.         struct Window* win = getDrawWin();
  137.         if(AslRequestTags(savereq, ASLFR_Window, win, TAG_DONE))
  138.         {
  139.             char filename[MAXFILENAME];
  140.             /* Create complete filename from ASL's dir and file */
  141.             strcpy(filename, savereq->rf_Dir);
  142.             if(AddPart(filename, savereq->rf_File, MAXFILENAME))
  143.             {
  144.                 struct EasyStruct myreq = { sizeof(struct EasyStruct),
  145.                                                                         0,
  146.                                                                         "File Overwrite Confirmation",
  147.                                                                         "The file %s already exists.\n"
  148.                                                                             "Do you want to overwrite it?",
  149.                                                                         "Yes|No" };
  150.                 int exists = FALSE;
  151.                 BPTR lock;
  152.                 /* Test whether the file exists by attempting a "Lock()" */
  153.                 if(lock = Lock(filename, ACCESS_READ))
  154.                 {
  155.                     exists = TRUE;
  156.                     /* Don't forget to "UnLock()" */
  157.                     UnLock(lock);
  158.                 }
  159.                 /* Only save in the suggested file if it's new or the user wants to */
  160.                 if(!exists || EasyRequest(win, &myreq, NULL, savereq->rf_File))
  161.                 {
  162.                     /* Make sure our bitmap is the same as the display */
  163.                     SyncSBitMap(win->WLayer);
  164.                     /* Try saving our bitmap, using the screen's colours */
  165.                     if(IFFL_SaveBitMap(filename, getBitmap(),
  166.                                                         win->WScreen->ViewPort.ColorMap->ColorTable,
  167.                                                         IFFL_COMPR_BYTERUN1))
  168.                     {
  169.                         /* Write an icon for the file */
  170.                         struct DiskObject* dobj = GetDefDiskObject(WBPROJECT);
  171.                         setModified(FALSE);
  172.                         if(dobj)
  173.                         {
  174.                             /* Temporarily change the default tool */
  175.                             char* dtool = dobj->do_DefaultTool;
  176.                             dobj->do_DefaultTool = progName();
  177.                             /* Write out our icon */
  178.                             PutDiskObject(filename, dobj);
  179.                             /* Reinstate the default tool */
  180.                             dobj->do_DefaultTool = dtool;
  181.                             FreeDiskObject(dobj);
  182.                         }
  183.                     }
  184.                     else
  185.                         printf("Error: could not write IFF picture\n");
  186.                 }
  187.             }
  188.             else
  189.                 printf("Error: could not make filename\n");
  190.         }
  191.         /* else: requester was cancelled */
  192.     }
  193.     else
  194.         printf("Error: could not allocate ASL (save) file request\n");
  195. }
  196.  
  197. /* Free any requesters that may have been allocated */
  198. void freeReqs()
  199. {
  200.     if(loadreq)
  201.     {
  202.         FreeAslRequest(loadreq);
  203.         loadreq = NULL;
  204.     }
  205.     if(savereq)
  206.     {
  207.         FreeAslRequest(savereq);
  208.         savereq = NULL;
  209.     }
  210. }
  211.  
  212.